package sms_api

import "time"

// API 端点常量
const (
	// Space IoT SMS API
	DefaultSpaceIoTBaseURL = "https://sms.space-iot.net/api"
	SpaceIoTSendAPI        = "/sms/sendByContent"
	SpaceIoTQueryAPI       = "/sms/query"

	// 阿里云 SMS API
	DefaultAlibabaRegion   = "cn-hangzhou"
	AlibabaAction          = "SendSms"
	AlibabaVersion         = "2017-05-25"
	AlibabaSignatureMethod = "HMAC-SHA1"
	AlibabaFormat          = "JSON"
)

// HTTP 状态码
const (
	StatusOK                  = 200
	StatusBadRequest          = 400
	StatusUnauthorized        = 401
	StatusForbidden           = 403
	StatusNotFound            = 404
	StatusTooManyRequests     = 429
	StatusInternalServerError = 500
	StatusBadGateway          = 502
	StatusServiceUnavailable  = 503
	StatusGatewayTimeout      = 504
)

// 错误代码
const (
	// 通用错误
	ErrCodeSuccess           = "0"
	ErrCodeInvalidParams     = "INVALID_PARAMS"
	ErrCodeInternalError     = "INTERNAL_ERROR"
	ErrCodeNetworkError      = "NETWORK_ERROR"
	ErrCodeTimeout           = "TIMEOUT"
	ErrCodeRateLimit         = "RATE_LIMIT"
	ErrCodeProviderError     = "PROVIDER_ERROR"
	ErrCodeValidationError   = "VALIDATION_ERROR"
	ErrCodeCircuitOpen       = "CIRCUIT_OPEN"

	// SMS 特定错误
	ErrCodeInvalidPhone      = "INVALID_PHONE"
	ErrCodeInvalidContent    = "INVALID_CONTENT"
	ErrCodeInvalidSign       = "INVALID_SIGN"
	ErrCodeSignatureError    = "SIGNATURE_ERROR"
	ErrCodeInsufficientQuota = "INSUFFICIENT_QUOTA"
	ErrCodeBlacklistPhone    = "BLACKLIST_PHONE"
	ErrCodeTemplateNotFound  = "TEMPLATE_NOT_FOUND"
	ErrCodeContentTooLong    = "CONTENT_TOO_LONG"
)

// 错误消息
const (
	ErrMsgInvalidParams     = "参数验证失败"
	ErrMsgInternalError     = "内部服务错误"
	ErrMsgNetworkError      = "网络连接错误"
	ErrMsgTimeout           = "请求超时"
	ErrMsgRateLimit         = "请求频率超限"
	ErrMsgProviderError     = "SMS提供商错误"
	ErrMsgValidationError   = "数据验证错误"
	ErrMsgCircuitOpen       = "熔断器开启，服务暂时不可用"
	ErrMsgInvalidPhone      = "手机号格式无效"
	ErrMsgInvalidContent    = "短信内容无效"
	ErrMsgInvalidSign       = "短信签名无效"
	ErrMsgSignatureError    = "请求签名错误"
	ErrMsgInsufficientQuota = "短信余额不足"
	ErrMsgBlacklistPhone    = "手机号在黑名单中"
	ErrMsgTemplateNotFound  = "短信模板不存在"
	ErrMsgContentTooLong    = "短信内容过长"
)

// SMS 提供商类型
const (
	ProviderSpaceIoT = "space_iot"
	ProviderAlibaba  = "alibaba"
	ProviderTencent  = "tencent"
	ProviderHuawei   = "huawei"
)

// 短信状态
const (
	StatusPending   = "pending"   // 待发送
	StatusSending   = "sending"   // 发送中
	StatusSent      = "sent"      // 已发送
	StatusDelivered = "delivered" // 已送达
	StatusFailed    = "failed"    // 发送失败
	StatusExpired   = "expired"   // 已过期
)

// 短信类型
const (
	SMSTypeVerification = "verification" // 验证码短信
	SMSTypeNotification = "notification" // 通知短信
	SMSTypeMarketing    = "marketing"    // 营销短信
	SMSTypeInternational = "international" // 国际短信
)

// 优先级
const (
	PriorityLow    = 1
	PriorityNormal = 2
	PriorityHigh   = 3
	PriorityUrgent = 4
)

// 重试配置常量
const (
	DefaultMaxAttempts    = 3
	DefaultInitialDelay   = 1 * time.Second
	DefaultMaxDelay       = 30 * time.Second
	DefaultBackoffFactor  = 2.0
	DefaultJitterFactor   = 0.1
	DefaultCircuitTimeout = 60 * time.Second
)

// 速率限制常量
const (
	DefaultRateLimitPerMinute = 100
	DefaultRateLimitPerHour   = 1000
	DefaultRateLimitPerDay    = 10000
)

// 批量处理常量
const (
	DefaultBatchSize      = 100
	MaxBatchSize          = 1000
	DefaultBatchTimeout   = 30 * time.Second
	DefaultWorkerPoolSize = 10
)

// 缓存配置
const (
	DefaultCacheTTL     = 5 * time.Minute
	DefaultCacheSize    = 10000
	CacheKeyPrefix      = "sms_api:"
	CacheKeyTemplate    = CacheKeyPrefix + "template:%s"
	CacheKeyQuota       = CacheKeyPrefix + "quota:%s"
	CacheKeyBlacklist   = CacheKeyPrefix + "blacklist:%s"
)

// 监控指标常量
const (
	MetricSMSSent        = "sms_sent_total"
	MetricSMSSuccess     = "sms_success_total"
	MetricSMSFailed      = "sms_failed_total"
	MetricSMSLatency     = "sms_latency_seconds"
	MetricSMSRetries     = "sms_retries_total"
	MetricCircuitState   = "sms_circuit_state"
	MetricRateLimitHits  = "sms_rate_limit_hits_total"
	MetricProviderErrors = "sms_provider_errors_total"
)

// 日志字段常量
const (
	LogFieldMessageID   = "message_id"
	LogFieldPhone       = "phone"
	LogFieldProvider    = "provider"
	LogFieldStatus      = "status"
	LogFieldLatency     = "latency"
	LogFieldAttempt     = "attempt"
	LogFieldErrorCode   = "error_code"
	LogFieldErrorMsg    = "error_message"
	LogFieldBatchID     = "batch_id"
	LogFieldBatchSize   = "batch_size"
)

// 配置键常量
const (
	ConfigKeyHost                    = "SMS_HOST"
	ConfigKeyPort                    = "SMS_PORT"
	ConfigKeyDebug                   = "SMS_DEBUG"
	ConfigKeyProvider                = "SMS_PROVIDER"
	ConfigKeySpaceIoTBaseURL         = "SMS_SPACE_IOT_BASE_URL"
	ConfigKeySpaceIoTSign            = "SMS_SPACE_IOT_SIGN"
	ConfigKeySpaceIoTKey             = "SMS_SPACE_IOT_KEY"
	ConfigKeyAlibabaAccessKeyID      = "SMS_ALIBABA_ACCESS_KEY_ID"
	ConfigKeyAlibabaAccessKeySecret  = "SMS_ALIBABA_ACCESS_KEY_SECRET"
	ConfigKeyAlibabaRegion           = "SMS_ALIBABA_REGION"
	ConfigKeyRetryMaxAttempts        = "SMS_RETRY_MAX_ATTEMPTS"
	ConfigKeyRetryInitialDelay       = "SMS_RETRY_INITIAL_DELAY"
	ConfigKeyRetryMaxDelay           = "SMS_RETRY_MAX_DELAY"
	ConfigKeyRateLimitPerMinute      = "SMS_RATE_LIMIT_PER_MINUTE"
	ConfigKeyRateLimitPerHour        = "SMS_RATE_LIMIT_PER_HOUR"
	ConfigKeyBatchSize               = "SMS_BATCH_SIZE"
	ConfigKeyWorkerPoolSize          = "SMS_WORKER_POOL_SIZE"
)

// 默认配置值
const (
	DefaultHost         = "localhost"
	DefaultPort         = 8080
	DefaultDebug        = false
	DefaultProvider     = ProviderSpaceIoT
	DefaultTimeout      = 30 * time.Second
	DefaultReadTimeout  = 10 * time.Second
	DefaultWriteTimeout = 10 * time.Second
)

// 验证规则常量
const (
	MaxPhoneLength   = 20
	MinPhoneLength   = 8
	MaxContentLength = 500
	MinContentLength = 1
	MaxSignLength    = 20
	MinSignLength    = 2
)

// 正则表达式模式
const (
	// 中国大陆手机号
	PhonePatternCN = `^1[3-9]\d{9}$`
	// 国际手机号（简化版）
	PhonePatternIntl = `^\+?[1-9]\d{1,14}$`
	// 短信签名（中文、英文、数字）
	SignPattern = `^[\u4e00-\u9fa5a-zA-Z0-9]{2,20}$`
)